package com.qeeyou.apps.android.craftsman.activity.me;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.po.CommonInfo;
import com.qeeyou.apps.android.craftsman.service.AppService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;

import java.util.HashMap;
import java.util.Map;

public class FeedbackActivity extends Activity {

    private ImageView btnBack;
    private EditText etOpinion;
    private EditText etMobile;
    private Button btnSubmit;

    private CustomLoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        
        setView();
        setEvent();
    }

    private void setView() {
        btnBack = (ImageView) this.findViewById(R.id.btnBack4Feedback);
        etOpinion = (EditText) this.findViewById(R.id.etOpinion4Feedback);
        etMobile = (EditText) this.findViewById(R.id.etMobile4Feedback);
        btnSubmit = (Button) this.findViewById(R.id.btnSubmit4Feedback);
    }

    private void setEvent() {
        btnBack.setOnClickListener(onBackClickListener);
        btnSubmit.setOnClickListener(onSubmitClickListener);
    }

    private View.OnClickListener onBackClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FeedbackActivity.this.finish();
        }
    };

    private View.OnClickListener onSubmitClickListener =  new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String feedContent = etOpinion.getText().toString();
            String feedPhone = etMobile.getText().toString();
            if(TextUtils.isEmpty(feedContent)){
                Toast.makeText(FeedbackActivity.this, "请输入意见反馈内容", Toast.LENGTH_SHORT).show();
                return;
            }
            if(TextUtils.isEmpty(feedPhone)){
                Toast.makeText(FeedbackActivity.this, "请输入意见反馈手机号", Toast.LENGTH_SHORT).show();
                return;
            }
            String params[] = {feedContent, feedPhone};
            new SubmitFeedbackAction().execute(params);
        }
    };

    //提交意见反馈
    class SubmitFeedbackAction extends AsyncTask<String, Void, CommonInfo> {
        public SubmitFeedbackAction() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if(mLoadingDialog == null) {
                mLoadingDialog = new CustomLoadingDialog(FeedbackActivity.this);
            }
            mLoadingDialog.setMessage("正在提交意见反馈...");
            mLoadingDialog.show();
        }

        @Override
        protected CommonInfo doInBackground(String... params) {
            String feedcontent = params[0];
            String feedphone = params[1];
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("feedcontent", feedcontent);
            paramMap.put("feedphone", feedphone);
            CommonInfo resultInfo = AppService.getInstance().feedbackAction(paramMap);
            return resultInfo;
        }

        @Override
        protected void onPostExecute(CommonInfo resultInfo) {
            super.onPostExecute(resultInfo);
            if(mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if(resultInfo != null && resultInfo.getStatus().equalsIgnoreCase("1")) {
                Toast.makeText(FeedbackActivity.this, "意见反馈提交成功", Toast.LENGTH_SHORT).show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        FeedbackActivity.this.finish();
                    }
                }, 1000);
            } else {
                Toast.makeText(FeedbackActivity.this, "意见反馈提交失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
